package com.bhs.sansonglogistics.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.bhs.sansonglogistics.R;
import com.bhs.sansonglogistics.base.Constants;
import com.bhs.sansonglogistics.ui.wallet.BalanceActivity;
import com.bhs.sansonglogistics.utils.ToastUtil;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import net.security.device.api.LogUtil;

import org.greenrobot.eventbus.EventBus;

/**
 * 微信充值回调页面
 */
public class WXPayEntryActivity extends Activity implements
        IWXAPIEventHandler {
    private IWXAPI api;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        api = WXAPIFactory.createWXAPI(this, Constants.APP_ID);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq baseReq) {
    }

    @Override
    public void onResp(BaseResp resp) {
        EventBus.getDefault().post(resp);
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            String msg = "";
            if (resp.errCode == BaseResp.ErrCode.ERR_OK) {
                msg = getString(R.string.pay_success);
            }  else if (resp.errCode == BaseResp.ErrCode.ERR_USER_CANCEL) {
                msg = getString(R.string.pay_cancel);
            }  else {
                msg = getString(R.string.label_pay_fail);
            }
            ToastUtil.show(msg);
        }
        finish();
    }
}
